package dbtask

import (
	"reflect"
	"github.com/astaxie/beego/orm"
	"fmt"
	"cloud_worm/util/umodels/mysqldb"
)

const (
	// 基于业务处理的模式
	WeaponTypeNeedAnalyzer = 0                   // weapon执行结果需要分析的类型
	WeaponTypeNoAnalyzer = 1                     // weapon执行结果不需要分析的类型
	WeaponTypeCareResult = 2                     // weapon执行的结果比较关心，eg：turtle相关的weapon的结果

	// 适用于weapon基于不同的任务模式下发
	WeaponTaskModeDefault = 0                       // 缺省的任务模式
	WeaponTaskModeBaseSec = 1                       // 用于基础的安全基线扫描，BaseSec
)

type Weapon struct {
	Id                  int64
	Name                string          // weapon名字
	Comment             string          // weapon简介

	Parameters          string          // weapon指标说明
	Md5                 string          // weapon的md5值
	TaskMode            int             // 任务策略
	Timeout             int             // 自定义weapon的超时
	Output              string          // weapon输出的格式
	Url                 string          // weapon下载的url
	Type                int             // weapon类型，classes类别

	UploadUser          string          //
	Created             int64
	Updated             int64
	Status              int
}

func (w *Weapon) TableName() string {
	return reflect.TypeOf(*w).Name()
}

func (w *Weapon) Read(fields ...string) error {
	err := orm.NewOrm().Read(w, fields...)
	if err != nil {
		return fmt.Errorf("%s, %s", w.TableName(), err.Error())
	}
	return nil
}

func (w *Weapon) Update(fields ...string) error {
	_, err := orm.NewOrm().Update(w, fields...)
	if err != nil {
		return fmt.Errorf("%s, %s", w.TableName(), err.Error())
	}
	return nil
}

func (w *Weapon) Insert() error {
	_, err := orm.NewOrm().Insert(w)
	if err != nil {
		return fmt.Errorf("%s, %s", w.TableName(), err.Error())
	}

	return nil
}

func init()  {
	orm.RegisterModel(new(Weapon))
}

// SelectByMode 选择weapon，基于指定的status、mode、weapon type等指标
func (w *Weapon) SelectByMode(offset int, limit int, status int, mode int, wpType int) ([]*Weapon, error) {
	var wps []*Weapon

	qs := orm.NewOrm().QueryTable(w).Offset(offset).Limit(limit)
	if status != mysqldb.StatusAll {
		qs = qs.Filter("status", status)
	}
	if mode != mysqldb.SelectIntAll {
		qs = qs.Filter("task_mode", mode)
	}
	if wpType != mysqldb.SelectIntAll {
		qs = qs.Filter("type", wpType)
	}

	_, err := qs.All(&wps)
	if err != nil {
		return nil, err
	}
	return wps, nil
}

/**
 根据条件选择
 */
func (w *Weapon) Select(offset int, limit int, status int, name string, uploadUser string, wpType int, wpMode int) ([]*Weapon, error) {
	var weapons []*Weapon

	qs := orm.NewOrm().QueryTable(w).Offset(offset).Limit(limit)
	if status != mysqldb.StatusAll {
		qs = qs.Filter("status", status)
	}
	if name != mysqldb.SelectStrAll {
		qs = qs.Filter("name__istartswith", name)
	}
	if uploadUser != mysqldb.SelectStrAll {
		qs = qs.Filter("upload_user__istartswith", uploadUser)
	}
	if wpType != mysqldb.SelectIntAll {
		qs = qs.Filter("type", wpType)
	}
	if wpMode != mysqldb.SelectIntAll {
		qs = qs.Filter("task_mode", wpMode)
	}
	qs = qs.OrderBy("type", "task_mode", "id")

	_, err := qs.All(&weapons)
	if err != nil {
		return nil, err
	}

	return weapons, nil
}

/**
 根据条件的count
 */
func (w *Weapon) Count(status int, name string, uploadUser string, wpType int, wpMode int) (int64, error) {

	qs := orm.NewOrm().QueryTable(w)
	if status != mysqldb.StatusAll {
		qs = qs.Filter("status", status)
	}
	if name != mysqldb.SelectStrAll {
		qs = qs.Filter("name__istartswith", name)
	}
	if uploadUser != mysqldb.SelectStrAll {
		qs = qs.Filter("upload_user__istartswith", uploadUser)
	}
	if wpType != mysqldb.SelectIntAll {
		qs = qs.Filter("type", wpType)
	}
	if wpMode != mysqldb.SelectIntAll {
		qs = qs.Filter("task_mode", wpMode)
	}

	count, err := qs.Count()
	if err != nil {
		return 0, err
	}

	return count, nil
}

// WolWeapon.SelectByNames 根据weapon name的数组获取weapon的信息
func (w *Weapon) SelectByNames(wpNames []string) ([]*Weapon, error) {
	var weapons []*Weapon

	qs := orm.NewOrm().QueryTable(w)
	// 有效的
	qs = qs.Filter("status", mysqldb.StatusValid)
	qs = qs.Filter("name__in", wpNames)

	_, err := qs.All(&weapons)
	if err != nil {
		return nil, err
	}

	return weapons, nil
}